package com.tianqi.modules.pms.service.impl;

import com.nlf.calendar.EightChar;
import com.nlf.calendar.Lunar;
import com.nlf.calendar.util.LunarUtil;
import com.tianqi.common.constant.LittleLiuRenConstant;
import com.tianqi.common.enums.JiaZiXunEnum;
import com.tianqi.common.enums.LittleLiuRenPanEnum;
import com.tianqi.common.utils.LunarDateUtil;
import com.tianqi.modules.pms.dto.request.DateTimeReq;
import com.tianqi.modules.pms.dto.response.LittleLiuRenResp;
import com.tianqi.modules.pms.service.LittleLiuRenService;
import com.tianqi.modules.tqs.constant.RelationMapConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 小六壬起课接口
 *
 * @author zhangzedi
 * @date 2023/8/21 21:37
 **/
@Service
@Slf4j
public class LittleLiuRenServiceImpl implements LittleLiuRenService {

	@Autowired
	private LunarDateUtil lunarDateUtil;

	/**
	 * 计算小六壬结果
	 *
	 * @param dateTimeReq
	 * @return
	 */
	@Override
	public List<LittleLiuRenResp> queryLittleLiuRen(DateTimeReq dateTimeReq) {
		// 获取阴历时间
		Lunar lunar = lunarDateUtil.formatEightChar(dateTimeReq);
		EightChar lunarEightChar = lunar.getEightChar();
		List<LittleLiuRenResp> littleLiuRenList = new ArrayList<>(6);
		for (int index = 1; index <= 6; index++) {
			LittleLiuRenResp littleLiuRen = new LittleLiuRenResp();
			// 设置地盘名称
			littleLiuRen.setGongName(LittleLiuRenPanEnum.getNameByIndex(index));
			littleLiuRenList.add(littleLiuRen);
		}
		//计算月宫位置
		int monthIndex = lunar.getMonth() % 6;
		if (monthIndex == 0) {
			monthIndex = 6;
		}
		// 余数减去1位下标
		monthIndex -= 1;
		littleLiuRenList.get(monthIndex).setMonthGong(true);
		// 计算日宫位置
		int dayRemainder = lunar.getDay() % 6;
		if (dayRemainder == 0) {
			dayRemainder = 6;
		}
		int dayIndex = monthIndex + dayRemainder - 1;
		// 判断是否大于6
		if (dayIndex > 6) {
			dayIndex -= 6;
		}
		littleLiuRenList.get(dayIndex).setDayGong(true);
		// 计算时宫位置
		int timeRemainder = (lunarEightChar.getDayZhiIndex() + 1) % 6;
		if (timeRemainder == 0) {
			timeRemainder = 6;
		}
		int timeIndex = dayIndex + timeRemainder - 1;
		if (timeIndex > 6) {
			timeIndex -= 6;
		}
		littleLiuRenList.get(timeIndex).setTimeGong(true);
		// 设置各个宫位的地支
		String yangDizhiList = LittleLiuRenConstant.YINGYANG_DIZHI.get(LittleLiuRenConstant.YANG);
		String yinDizhiList = LittleLiuRenConstant.YINGYANG_DIZHI.get(LittleLiuRenConstant.YIN);
		String dizhi;
		// 获取时支判断阴阳
		if (yangDizhiList.contains(lunarEightChar.getTimeZhi())) {
			// 获取地支的坐标
			int timeZhiIndex = yangDizhiList.indexOf(lunarEightChar.getTimeZhi());
			int times = 0;
			for (int shenIndex = timeIndex; shenIndex < 6; shenIndex++) {
				if ((timeZhiIndex + times) < 6) {
					dizhi = String.valueOf(yangDizhiList.charAt(timeZhiIndex + times));
				} else {
					dizhi = String.valueOf(yangDizhiList.charAt(timeZhiIndex + times - 6));
				}
				littleLiuRenList.get(shenIndex).setZhi(dizhi);
				times++;
			}
			for (int shenIndex = 0; shenIndex < timeIndex; shenIndex++) {
				if ((timeZhiIndex + times) < 6) {
					dizhi = String.valueOf(yangDizhiList.charAt(timeZhiIndex + times));
				} else {
					dizhi = String.valueOf(yangDizhiList.charAt(timeZhiIndex + times - 6));
				}
				littleLiuRenList.get(shenIndex).setZhi(dizhi);
				times++;
			}
		} else {
			// 获取地支的坐标
			int timeZhiIndex = yinDizhiList.indexOf(lunarEightChar.getTimeZhi());
			int times = 0;
			for (int shenIndex = timeIndex; shenIndex < 6; shenIndex++) {
				if ((timeZhiIndex + times) < 6) {
					dizhi = String.valueOf(yinDizhiList.charAt(timeZhiIndex + times));
				} else {
					dizhi = String.valueOf(yinDizhiList.charAt(timeZhiIndex + times - 6));
				}
				littleLiuRenList.get(shenIndex).setZhi(dizhi);
				times++;
			}
			for (int shenIndex = 0; shenIndex < timeIndex; shenIndex++) {
				if ((timeZhiIndex + times) < 6) {
					dizhi = String.valueOf(yinDizhiList.charAt(timeZhiIndex + times));
				} else {
					dizhi = String.valueOf(yinDizhiList.charAt(timeZhiIndex + times - 6));
				}
				littleLiuRenList.get(shenIndex).setZhi(dizhi);
				times++;
			}
		}
		String dayXunKong = lunarEightChar.getDayXunKong();
		// 根据日干支去确定天干
		int xunIndex = 0;
		for (int i = 0; i < LunarUtil.XUN.length; i++) {
			if (LunarUtil.XUN[i].equals(lunarEightChar.getDayXun())) {
				// 获取旬的下标
				xunIndex = i;
				break;
			}
		}
		// 通过旬的下标获取旬中所有的甲子以及下一个旬的所有数据
		List<JiaZiXunEnum> ganZhiByXunIndex = JiaZiXunEnum.getGanZhiByXunIndex(xunIndex + 1);
		List<JiaZiXunEnum> ganZhiByXunIndexNext;
		if (xunIndex == 5) {
			ganZhiByXunIndexNext = JiaZiXunEnum.getGanZhiByXunIndex(1);
		} else {
			ganZhiByXunIndexNext = JiaZiXunEnum.getGanZhiByXunIndex(xunIndex + 2);
		}
		//日宫的位置

		// 根据时干和各个宫位的地支确定六亲
		for (LittleLiuRenResp littleLiuRenResp : littleLiuRenList) {
			littleLiuRenResp.setLiuQin(RelationMapConstant.DIZHI_DIZHILIUQIN_MAP.get(lunarEightChar.getTimeZhi() +
					littleLiuRenResp.getZhi()));
			if (littleLiuRenResp.getZhi().equals(lunarEightChar.getTimeZhi())) {
				littleLiuRenResp.setLiuQin("自身");
			}
			// 设置地支的五行
			littleLiuRenResp.setZhiWuXing(LunarUtil.WU_XING_ZHI.get(littleLiuRenResp.getZhi()));
			// 根据地支获取六神
			littleLiuRenResp.setLiuShen(LittleLiuRenConstant.DIZHI_LIUSHEN.get(littleLiuRenResp.getZhi()));
			// 根据地支获取天干
			// 先判断是不是旬空的地支
			if (dayXunKong.contains(littleLiuRenResp.getZhi())) {
				for (JiaZiXunEnum jiaZiXunEnum : ganZhiByXunIndexNext) {
					if (StringUtils.isNotEmpty(jiaZiXunEnum.getGanByZhi(littleLiuRenResp.getZhi()))) {
						littleLiuRenResp.setGan(jiaZiXunEnum.getGanByZhi(littleLiuRenResp.getZhi()));
						// 根据天干设置五行
						littleLiuRenResp.setGanWuXing(LunarUtil.WU_XING_GAN.get(littleLiuRenResp.getGan()));
						break;
					}
				}
			} else {
				for (JiaZiXunEnum jiaZiXunEnum : ganZhiByXunIndex) {
					if (StringUtils.isNotEmpty(jiaZiXunEnum.getGanByZhi(littleLiuRenResp.getZhi()))) {
						littleLiuRenResp.setGan(jiaZiXunEnum.getGanByZhi(littleLiuRenResp.getZhi()));
						// 根据天干设置五行
						littleLiuRenResp.setGanWuXing(LunarUtil.WU_XING_GAN.get(littleLiuRenResp.getGan()));
						break;
					}
				}
			}
		}
		int wuXingTimes = 0;
		// 根据日宫确定五星位置
		for (int i = dayIndex; i < 6; i++) {
			LittleLiuRenResp littleLiuRenResp = littleLiuRenList.get(i);
			littleLiuRenResp.setFiveStar(LittleLiuRenConstant.WUXING.substring(wuXingTimes,
					wuXingTimes + 2));
			wuXingTimes += 2;
		}
		for (int i = 0; i < dayIndex; i++) {
			LittleLiuRenResp littleLiuRenResp = littleLiuRenList.get(i);
			littleLiuRenResp.setFiveStar(LittleLiuRenConstant.WUXING.substring(wuXingTimes,
					wuXingTimes + 2));
			wuXingTimes += 2;
		}
		return littleLiuRenList;
	}
}
